package com.hkts.management.cargoViewRecord.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.cargoViewRecord.entity.HktsCargoviewRecord;
import com.hkts.management.cargoViewRecord.mapper.CargoViewRecordMapper;
import com.hkts.management.cargoViewRecord.service.ICargoViewRecordService;
import com.hkts.management.common.utils.SnowFlakeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-04-28
 */
@Service
@Transactional
public class CargoViewRecordServiceImpl extends ServiceImpl<CargoViewRecordMapper, HktsCargoviewRecord> implements ICargoViewRecordService {


    @Autowired
    private CargoViewRecordMapper cargoViewRecordMapper;

    @Override
    public IPage<HktsCargoviewRecord> selectPage(Page<HktsCargoviewRecord> page, HktsCargoviewRecord cargoViewRecord) {
        QueryWrapper<HktsCargoviewRecord> query = Wrappers.query();
        QueryWrapper<HktsCargoviewRecord> entityWrapper = getEntityWrapper(query, cargoViewRecord);
        return cargoViewRecordMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsCargoviewRecord> selectList(HktsCargoviewRecord cargoViewRecord) {
        QueryWrapper<HktsCargoviewRecord> query = Wrappers.query();
        QueryWrapper<HktsCargoviewRecord> entityWrapper = getEntityWrapper(query, cargoViewRecord);
        return cargoViewRecordMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsCargoviewRecord> getEntityWrapper(QueryWrapper<HktsCargoviewRecord> entityWrapper, HktsCargoviewRecord cargoViewRecord) {
        //条件拼接
        if (StringUtils.isNotBlank(cargoViewRecord.getCargoId())) {
            entityWrapper.like(HktsCargoviewRecord.CARGO_ID, cargoViewRecord.getCargoId());
        }
        if (StringUtils.isNotBlank(cargoViewRecord.getDriverId())) {
            entityWrapper.like(HktsCargoviewRecord.DRIVER_ID, cargoViewRecord.getDriverId());
        }

        return entityWrapper;
    }

    @Override
    public int getViewRecordCount(String cargoId) {
        QueryWrapper<HktsCargoviewRecord> query = Wrappers.query();
        query.lambda().eq(HktsCargoviewRecord::getCargoId,cargoId);
        int count=cargoViewRecordMapper.selectCount(query);
        return count;
    }

    @Override
    public int addcargoViewRecord(HktsCargoviewRecord cargoViewRecord) {
        QueryWrapper<HktsCargoviewRecord> query = Wrappers.query();
        query.lambda().eq(HktsCargoviewRecord::getCargoId,cargoViewRecord.getCargoId())
                .eq(HktsCargoviewRecord::getDriverId,cargoViewRecord.getDriverId());
        int count=cargoViewRecordMapper.selectCount(query);
        if(count==0){
            cargoViewRecord.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            cargoViewRecordMapper.insert(cargoViewRecord);
        }
        int result=getViewRecordCount(cargoViewRecord.getCargoId());
        return result;
    }
}
